# coding:utf-8
from datetime import date, datetime
from typing import Union, List

from fastapi import Query
from pydantic import BaseModel, Field


class AdminRegister(BaseModel):
    """注册"""
    username: str = Field(title="用户名", min_length=2, max_length=32)
    password: str = Field(title="密码", min_length=2, max_length=32)
    confirm_password: str = Field(title="确认密码", min_length=2, max_length=32)


class AdminListIn(BaseModel):
    """列表参数"""
    keyword: str = Field(default='')  # 搜索关键词
    start_time: Union[date, datetime, None] = Query(alias='startTime')  # 开始时间
    end_time: Union[date, datetime, None] = Query(alias='endTime')  # 结束时间


class AdminBase(BaseModel):
    username: str
    nickname: str
    role_ids: str
    status: int


class AdminCreate(BaseModel):
    username: str
    nickname: str
    password: str = Field(default="")
    role_ids: str
    status: int


class AdminUpdate(AdminCreate):
    id: int


class AdminOutDB(AdminBase):
    """
    管理员信息输出
    """
    id: int  # 主键
    createTime: datetime = Field(alias='createTime')
    updateTime: datetime = Field(alias='updateTime')

    class Config:
        from_attributes = True
        exclude = ['password']  # 排除密码字段


class RoleBase(BaseModel):
    name: str
    remark: str = Field(default=None)
    sort: int
    status: int


class RoleUpdate(RoleBase):
    id: int


# 更新用户菜单
class RoleMenuUpdate(BaseModel):
    role_id: int
    menu_ids: List[int]


# 菜单
class SystemMenuOutDB(BaseModel):
    """系统菜单返回信息"""
    id: int  # 主键
    pid: int  # 上级菜单
    menuType: str = Field(alias='menu_type')  # 权限类型: [M=目录, C=菜单, A=按钮]
    menuName: str = Field(alias='menu_name')  # 菜单名称
    menuIcon: str = Field(alias='menu_icon')  # 菜单图标
    sort: int  # 菜单排序
    scopes: str  # 权限标识
    paths: str  # 路由地址
    component: str  # 前端组件
    params: str  # 路由参数
    isCache: int = Field(alias='is_cache')  # 是否缓存: [0=否, 1=是]
    isShow: int = Field(alias='is_show')  # 是否显示: [0=否, 1=是]
    status: int  # 是否禁用: [0=否, 1=是]
    createTime: datetime = Field(alias='create_time')  # 创建时间
    updateTime: datetime = Field(alias='update_time')  # 更新时间
    children: Union['SystemMenuOutDB', None]  # 子集


class SystemMenuBase(BaseModel):
    """系统菜单返回信息"""
    pid: int  # 上级菜单
    menu_type: str  # 权限类型: [M=目录, C=菜单, A=按钮]
    menu_name: str  # 菜单名称
    menu_icon: str = Field(default=None)  # 菜单图标
    sort: int = Field(default=0)  # 菜单排序
    scopes: str = Field(default=None)  # 权限标识
    paths: str = Field(default=None)  # 路由地址
    component: str = Field(default=None)  # 前端组件
    params: str = Field(default=None)  # 路由参数
    is_cache: int  # 是否缓存: [0=否, 1=是]
    is_show: int  # 是否显示: [0=否, 1=是]
    status: int  # 是否禁用: [0=否, 1=是]


class SystemMenuUpdate(SystemMenuBase):
    id: int  # 主键


# 岗位
class PostBase(BaseModel):
    name: str
    code: str
    remark: str = Field(default=None)
    sort: int
    status: int


class PostUpdate(PostBase):
    id: int


# 部门
class DeptBase(BaseModel):
    pid: int = Field(default=0)
    name: str
    leader: str = Field(default=None)
    mobile: str = Field(default=None)
    sort: int
    status: int
    remark: str = Field(default=None)


class DeptUpdate(RoleBase):
    id: int
